package com.my.job;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.entity.SchedulerValue;
import com.my.entity.TestPaper;
import com.my.service.SchedulerValueService;
import com.my.service.TestPaperService;
import com.my.util.ApplicationContextHolder;
import com.my.util.ScheduleUtil;

public class TestPaperJob {

    private ScheduleUtil scheduleUtil;

    private SchedulerValueService svService;

    private TestPaperService tpService;

    public void setScheduleUtil() {
        this.scheduleUtil = ApplicationContextHolder.getBean("scheduleUtil");
    }

    public void setSvService() {
        this.svService = ApplicationContextHolder.getBean("schedulerValueService");
    }

    public void setTpService() {
        this.tpService = ApplicationContextHolder.getBean("testPaperService");
    }

    public void execution(JSONObject obj) {
        if(scheduleUtil == null) {
            setScheduleUtil();
        }
        if(svService == null) {
            setSvService();
        }
        if(tpService == null) {
            setTpService();
        }
        Long id = obj.getLong("id");
        tpService.addStudentTestPaper(id);
        tpService.updateById(new TestPaper().setId(id).setStatus(3));
        svService.remove(new QueryWrapper<SchedulerValue>().eq("uuid", obj.getStr("uuid")));
        scheduleUtil.deleteJob(JSONUtil.toBean(obj.getStr("schedulerValue"), SchedulerValue.class));
    }

}
